package com.wise.service.cms.content.category;

import cn.hutool.core.collection.CollectionUtil;
import com.wise.entities.content.Product;
import com.wise.entities.content.ProductCategory;
import com.wise.framework.dao.SqlSelect;
import com.wise.framework.daomodel.DaoList;
import com.wise.framework.daomodel.DaoOne4Update;
import com.wise.framework.daomodel.DaoRemove;
import com.wise.framework.validation.Precondition;
import com.wise.summer.v1.cms.content.category.DeleteCategoryById;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

import java.util.List;

import static com.wise.model.content.ContentErrMsgs.Err_001000250007;
import static com.wise.model.content.ContentErrMsgs.Err_001000250041;

/**
 * @author shaoming
 * @date 2019-09-10
 */
@Service
@SummerService
public class DeleteCategoryByIdService implements SummerServiceBean<DeleteCategoryById> {

	@Override
	public void sum(DeleteCategoryById summer) {
		ProductCategory productCategory = DaoOne4Update.s(ProductCategory.class, summer.getId(), Err_001000250007);

		List<Product> productList = getProductByCategoryId(summer.getId());
		Precondition.checkState(CollectionUtil.isEmpty(productList), Err_001000250041);

		DaoRemove.s(productCategory);
		summer.setSummerResult(Boolean.TRUE);
	}

	/**
	 * 获取指定分类下的所有产品
	 *
	 * @param categoryId
	 * @return
	 */
	private List<Product> getProductByCategoryId(String categoryId) {
		SqlSelect sqlSelect = SqlSelect.selec("*")
				.fromClassName(Product.class)
				.eq(Product._categoryId, categoryId);

		return DaoList.s(Product.class, sqlSelect);
	}
}
